package com.android.delivery.base;

import android.os.Bundle;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.ada.common.util.CollectionUtil;
import com.blankj.utilcode.util.ObjectUtils;
import com.android.delivery.R;
import com.android.delivery.constant.IntentKeyConstant;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;

import butterknife.BindView;

/**
 * Created by mqwang on 2017/12/6.
 */

public abstract class BaseListActivity<T> extends BaseActivity implements OnRefreshLoadMoreListener {
    @BindView(R.id.recyclerView)
    RecyclerView recLv;
    @BindView(R.id.refreshLayout)
    protected
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.layout_empty)
    LinearLayout layoutEmpty;;
//    @BindView(R.id.iv_no_data)
//    ImageView ivNoData;
//    @BindView(R.id.tv_no_data)
//    TextView tvNoData;
    @BindView(R.id.root_layout)
    public LinearLayout rootLayout;;

    private int page = 1;

    @Override
    protected int getLayoutId() {
        return R.layout.include_refresh_layout_list;
    }

    @Override
    protected void init(Bundle savedInstanceState, View container) {
//        recLv.setLayoutManager(new LinearLayoutManager(this));
        bindRecyclerView(recLv);
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadMoreListener(this);

        //这个有空再看下
//        layoutEmpty.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
////                layoutEmpty.setVisibility(View.GONE);
//                autoRefresh();
//
//            }
//        });

        onRefresh(refreshLayout);
    }

    protected abstract void bindRecyclerView(RecyclerView recLv);
    protected abstract void loadData(int page, int pageCount);
    protected abstract List<T> getData();
    protected abstract void bindData(boolean isRefresh, List<T> list);


    /** 主动调用刷新 */
    public void autoRefresh(){
        refreshLayout.autoRefresh();
        page = 1;

    }

    @Override
    public void onLoadMore(RefreshLayout refreshlayout) {
        loadData(page, IntentKeyConstant.PAGE_COUNT);

    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        page = 1;
        loadData(page, IntentKeyConstant.PAGE_COUNT);

    }

    /**
     * 拉取数据成功时调用
     * @param data 数据源
     */
    protected void onGetDataSuc(List<T> data){
        finishRefresh(refreshLayout);
        bindData(isRefresh(), data);
        if (!ObjectUtils.isEmpty(data)){
            page ++ ;
        }
//        else{//判断是否显示无数据界面
//            checkData();
//
//        }
        checkData();

        if (CollectionUtil.getSize(data) < IntentKeyConstant.PAGE_COUNT){//无更多
            refreshLayout.setNoMoreData(true);//将不会再次触发加载更多事件
        }else{
            refreshLayout.setNoMoreData(false);//还有更多
        }

    }

    private void checkData(){
        if (!ObjectUtils.isEmpty(getData())){
            layoutEmpty.setVisibility(View.GONE);
        } else{
            layoutEmpty.setVisibility(View.VISIBLE);
        }

    }

    public void setGoneEmpty(){
//        layoutEmpty.setVisibility(View.GONE);

    }

    /** 拉取数据失败时调用 */
    protected final void onGetDataFail(){
        finishRefresh(refreshLayout);

    }

//    public void setEmptyDesc(String desc, int source){
//        if (!ObjectUtils.isEmpty(desc)) {
//            tvNoData.setText(desc);
//        }
//        if (source > 0) {
//            ivNoData.setImageResource(source);
//        }
//
//    }

    private boolean isRefresh(){
        return page == 1;
    }


}
